package com.uniknow.app.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uniknow.app.R;
import com.uniknow.app.bin.UserBean;
import com.uniknow.app.operate.BaseListener;
import com.uniknow.app.operate.LoginOperate;
import com.uniknow.app.operate.impl.LoginOperateImpl;
import com.uniknow.app.util.UIHelper;

public class LoginActivity extends BaseActivity{
	//tableView 
	private int TB_COUNT;
	private LinearLayout[] listTableView;
	private static ArrayList<LinearLayout> LayoutManage  = new ArrayList<LinearLayout>();

	private Button IndexLoginBtn;
	private TextView IndexRegisBtn,IndexFreeBtn,IndexForgetBtn,RegisNext,RegisBack,ForgetNext,ForgetBack,CaptchaBack,CaptchaDone,StartStart;
	private EditText IndexUserName,IndexPasswd;
	
	private LinearLayout LayoutBody,LayoutIndex,LayoutRegis,LayoutForget,LayoutCaptcha,LayoutStart;
	
	private LoginOperate loginOperate;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		loginOperate = new LoginOperateImpl(this);
//		if (android.os.Build.VERSION.SDK_INT > 9) {
//		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//		    StrictMode.setThreadPolicy(policy);
//		}
		LayoutManage.clear();
		initView();
	}
	/**
	 * 初始化界面
	 */
	private void initView(){
		LayoutBody = (LinearLayout) findViewById(R.id.login_layout);
		LayoutIndex = (LinearLayout) LayoutBody.findViewById(R.id.activity_login_index);
		LayoutRegis = (LinearLayout) LayoutBody.findViewById(R.id.activity_login_regis);
		LayoutForget = (LinearLayout) LayoutBody.findViewById(R.id.activity_login_forget);
		LayoutCaptcha = (LinearLayout) LayoutBody.findViewById(R.id.activity_login_captcha);
		LayoutStart = (LinearLayout) LayoutBody.findViewById(R.id.activity_login_start);
		
		IndexUserName = (EditText) findViewById(R.id.login_index_user_name);
		IndexPasswd = (EditText) findViewById(R.id.login_index_pass_word);
		IndexLoginBtn = (Button) findViewById(R.id.login_index_login_btn);
		IndexRegisBtn = (TextView) findViewById(R.id.login_index_login_regis);
		IndexFreeBtn = (TextView) findViewById(R.id.login_index_login_free);
		IndexForgetBtn = (TextView) findViewById(R.id.login_index_login_forget);
		RegisNext = (TextView) findViewById(R.id.login_regis_next);
		RegisBack = (TextView) findViewById(R.id.login_regis_back);
		ForgetNext = (TextView)findViewById(R.id.login_forget_next);
		ForgetBack = (TextView)findViewById(R.id.login_forget_back);
		CaptchaBack = (TextView)findViewById(R.id.login_captcha_back);
		CaptchaDone = (TextView)findViewById(R.id.login_captcha_done);
		StartStart = (TextView)findViewById(R.id.login_start_start);
		
		IndexLoginBtn.setOnClickListener(BtnClick());
		IndexRegisBtn.setOnClickListener(BtnClick());
		IndexFreeBtn.setOnClickListener(BtnClick());
		IndexForgetBtn.setOnClickListener(BtnClick());
		RegisNext.setOnClickListener(BtnClick());
		RegisBack.setOnClickListener(BtnClick());
		ForgetNext.setOnClickListener(BtnClick());
		ForgetBack.setOnClickListener(BtnClick());
		CaptchaBack.setOnClickListener(BtnClick());
		CaptchaDone.setOnClickListener(BtnClick());
		StartStart.setOnClickListener(BtnClick());
		
		TB_COUNT = LayoutBody.getChildCount();
		listTableView = new LinearLayout[TB_COUNT];
		for(int i=0;i<TB_COUNT;i++){
			listTableView[i] = (LinearLayout) LayoutBody.getChildAt(i);
			if(i!=0){
				listTableView[i].setVisibility(View.GONE);
			}
		}
		LayoutManage.add(listTableView[0]);
		IndexUserName.setInputType(InputType.TYPE_CLASS_NUMBER);
		IndexUserName.setFocusable(true);
		IndexUserName.setFocusableInTouchMode(true);
		IndexUserName.requestFocus();
        //延迟弹出键盘
//        Timer timer = new Timer();
//	    timer.schedule(new TimerTask(){
//	         public void run(){
//	        	 InputMethodManager inputManager = (InputMethodManager)IndexUserName.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//	             inputManager.showSoftInput(IndexUserName, 0);
//	         }
//	     }, 800);
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
        
	}



	/**
	 * 单击事件集合
	 * @param btn<Object>
	 * @return
	 */
	private View.OnClickListener BtnClick(){
		return new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				switch(v.getId()){
				case R.id.login_index_login_btn:
					/*Intent intent2  = new Intent();
					intent2.setClass(LoginActivity.this, Main.class);
					LoginActivity.this.startActivity(intent2);*/
					
					System.out.println("点击登录按钮");
					//if (!getSharedPreferences(AppConfig.SHAREDPREFERENCES_NAME, MODE_PRIVATE).getBoolean("isBinding", true)) {
					final UserBean user  = new UserBean();
					user.setUserName(IndexUserName.getText().toString());
					user.setPassWord(IndexPasswd.getText().toString());
					showLoading();
					loginOperate.login(user, new BaseListener() {
						
						@Override
						public void onSuccess(Object o) {
							//UIHelper.ToastMessage(LoginActivity.this, o.toString());
							Intent intent  = new Intent();
							intent.setClass(LoginActivity.this, MainActivity.class);
							LoginActivity.this.startActivity(intent);
							closeLoading();
						}
						
						@Override
						public void onError(String e) {
							UIHelper.ToastMessage(LoginActivity.this, e);
							closeLoading();
						}
					});
					
					//}else{
					//	GOSomeOneView(LayoutStart);
					//}
					break;
				case R.id.login_index_login_regis:
					GOSomeOneView(LayoutRegis);
					break;
				case R.id.login_index_login_free:
					UIHelper.ToastMessage(LoginActivity.this, "暂时未定");
					break;
				case R.id.login_index_login_forget:
					GOSomeOneView(LayoutForget);
					break;
				case R.id.login_forget_back:
					ViewBack();
					break;
				case R.id.login_forget_next:
					GOSomeOneView(LayoutCaptcha);
					break;
				case R.id.login_regis_next:
					GOSomeOneView(LayoutCaptcha);
					break;
				case R.id.login_regis_back:
					ViewBack();
					break;
				case R.id.login_captcha_back:
					ViewBack();
					break;
				case R.id.login_captcha_done:
					ViewBack(0);
					break;
				case R.id.login_start_start:
//					Intent intent  = new Intent();
//					intent.setClass(LoginActivity.this, SmartBoxBindingActivity.class);
//					LoginActivity.this.startActivity(intent);
//					LoginActivity.this.finish();
					break;
				}
				
			}
		};
	}
	/**
	 * 显示某个View
	 * @param layout
	 */
	private void GOSomeOneView(LinearLayout layout){
		for(int i=0;i<TB_COUNT;i++){
			listTableView[i].setVisibility(View.GONE);
		}
		layout.setVisibility(View.VISIBLE);
		LayoutManage.add(layout);
		System.out.println("statc = "+LayoutManage.size());
	}
	private void ViewBack(){
		ViewBack(1);
	}
	private void ViewBack(int pos){
		if(pos==0){
			for(int i=LayoutManage.size()-1;i>0;i--){
				LayoutManage.get(i).setVisibility(View.GONE);
				LayoutManage.remove(i);
			}
			LayoutManage.get(0).setVisibility(View.VISIBLE);
		}else{
			if(LayoutManage.size()==1){
				this.finish();
			}else{
				LayoutManage.get(LayoutManage.size()-1).setVisibility(View.GONE);
				LayoutManage.remove(LayoutManage.size()-1);
				LayoutManage.get(LayoutManage.size()-1).setVisibility(View.VISIBLE);
			}
		}
	}
	/**
	 * 监听功能键（返回--是否退出程序）
	 * @return 
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = true;
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			//是否退出应用
			ViewBack();
		}else{
			flag = super.onKeyDown(keyCode, event);
		}
		return flag;
	}
	

}
